import time 
import json
import uuid 
import string
import hashlib
# +----------------------------------------------------------------------
# | Jumper
# +----------------------------------------------------------------------
# | 消息任务
# | 发送下队列消息的命令为Queue::sendNow('artisan.order.test',array('data'=>'ttt'))
# | artisan.order.test中artisan为模块名，order为控制器，test为rder的成员方法
# +----------------------------------------------------------------------
# | @author: Asun
# | @version 1.0
# | @copyright (c) 2006~2017 Sltech All rights reserved.
# +----------------------------------------------------------------------
#**
#* Class Task 消息任务
#* @package jumper
#*
class Task:
	#**
	#* 构造函数
	#* @access   public
	#* @param mixed command 命令
	#* @param mixed data	数据
	#*/
	def __init__(self,command='', data = ''):
		# 原始数据
		self.data=data
		#命令
		self.command=command
		#任务类型
		self.type=''
		#执行计划：定时任务,onece,一次性立即,delay_loop每隔多少秒执行一次,time_loop每个时间点去执行,
		self.plan=''
		#下次执行时间
		self.next_time=''
		self.interval=0
		#执行次数
		self.times=0
		#状态
		self.status=0
		#签名
		self.sign=''
		#多长时间内允许重复执行
		self.unique_time = 0
		#消息唯一ID
		self.uuid=''
		self.error_times=0	
	
	#**
	# Json字符串生成任务
	# @access public
	# @return mixed
	#/
	def fromJson(self,s):
		data=json.loads(s)

		if data and data.get('command',False):
			self.command=data.get('command','')
			self.type=data.get('type','')
			self.plan=data.get('plan','')
			self.next_time=data.get('next_time',0)
			self.unique_time=data.get('unique_time',0)
			self.interval=data.get('interval',0)
			self.times=data.get('times',0)
			self.sign=data.get('sign','')
			self.uuid=data.get('uuid','')
			self.data=data.get('data',None)
			if data.get('error_times',0):
				self.error_times=data.get('error_times',0)
			else:
				self.error_times=0
	
	#*
	# 任务转换成Json字符串
	# @access public
	# @return string
	#/
	def toJson(self):
		ret={}
		ret['command']=self.command
		ret['type']=self.type
		ret['plan']=self.plan
		ret['next_time']=self.next_time
		ret['interval']=self.interval
		ret['times']=self.times
		ret['uuid']=self.uuid
		ret['data']=self.data
		if self.error_times>0:
			ret['error_times']=self.error_times
		
		#生成签名
		ret['sign']=self.sign
		return json.dumps(ret)
	
	def makeUUID(self):
		self.uuid   = str(uuid.uuid4())
	
	#转换成数组
	def toArray(self):
		ret={}
		for k,v in self.data.items():
			ret['data.'+k]=v
		ret['command']=self.command
		ret['type']=self.type
		ret['plan']=self.plan
		ret['next_time']=self.next_time
		ret['interval']=self.interval
		ret['times']=self.times
		ret['uuid']=self.uuid
		
		if self.error_times>0:
			ret['error_times']=self.error_times
		
		#生成签名
		ret['sign']=self.sign
		return ret
	#**
	#* 更新字段数据
	#* @param mixed fields
	#*/	
	def update(self,fields):
		self.data.update(fields)
	#**
	#* 遇到错误
	#* @param mixed times
	#*/
	def makeError(self):
		self.times=self.times+1
		self.error_times = self.error_times+1
		return self.error_times
		
	#*
	#  生成签名
	# @return bool
	#/
	def makeHash(self):
		ret={}
		ret['command']=self.command
		ret['type']=self.type
		ret['plan']=self.plan
		ret['next_time']=self.next_time
		ret['interval']=self.interval
		ret['times']=self.times
		ret['data']=self.data
		data = json.dumps(ret)
		return hashlib.md5(data.encode(encoding='UTF-8')).hexdigest()

	#*
	#  验证签名
	# @return bool
	#/
	def checkSign(self):
		return True
	

	def checkTimes(self):
		self.times=self.times-1
		if self.times>0 and self.plan=='loop_delay':
			self.next_time=self.next_time+self.interval
		
		return self.times>=0
	
	#*
	#  生成签名
	# @return bool
	#/
	def makeSign(self):
		return True
	
	#*
	# 生成立即执行的任务
	# @param command
	# @param data
	# @return Task
	#/
	@staticmethod
	def createNow(command, data = None):
		task=Task(command,data)
		task.plan='now'
		task.type='worker'
		task.times=1
		task.status=0
		task.makeUUID()
		return task

	#*
	# 生成计划任务
	# @param command
	# @param time
	# @param data
	# @return Task
	#/
	@staticmethod
	def createAt(command, time, data = None):
		task=Task(command,data)
		task.plan='at'
		task.type='worker'
		task.next_time=time
		task.times=1
		task.status=0
		task.makeUUID()
		return task

	#*
	# 创建定时循环任务
	# @param command
	# @param sec 执行时间间隔,秒为单位
	# @param loop
	# @param null data
	# @return Task
	#/
	@staticmethod
	def createDelay(command, time,sec,loop, data = None):
		task=Task(command,data)
		task.plan='loop_delay'
		task.type='worker'
		task.next_time=time
		task.interval=sec
		task.times=loop
		task.status=0
		task.makeUUID()
		return task
	
	#*
	# 从JOSN串中生成任务
	# @param json
	# @return Task
	#/
	@staticmethod
	def createFromJson(json):
		task=Task()
		task.fromJson(json)
		return task
